﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using AmazeOn.Models;
using System.Collections.Generic;
using System;
using System.Linq;

namespace AmazeOn.Content
{
    public class AdminLogViewModel : ViewModelBase
    {
        private List<Item> allItems;
        public List<Item> CurrentItems { get; set; }
        private int countOfItemsInCart;        

        private int selectedItem;
        public int SelectedItem 
        {
            get
            {
                return this.selectedItem;
            }
            set
            {
                this.selectedItem = value;
                
                RaisePropertyChanged("SelectedItem");
                RaisePropertyChanged("Model");
                RaisePropertyChanged("Price");
                RaisePropertyChanged("Picture");
            }
        }

        public AdminLogViewModel()
        {
            this.allItems = Store.FillUpStore();
            this.CurrentItems = this.allItems;

            showAll = new RelayCommand(() => ShowAll());
            showComputers = new RelayCommand(() => ShowComputers());
            showLaptops = new RelayCommand(() => ShowLaptops());
            showGSMs = new RelayCommand(() => ShowGSMs());
            addToCart = new RelayCommand(() => AddToCart());
            goToCart = new RelayCommand(() => GoToCart());
            login = new RelayCommand(() => Login());
            
        }

        public RelayCommand showAll { get; set; }
        public RelayCommand showComputers { get; set; }
        public RelayCommand showLaptops { get; set; }
        public RelayCommand showGSMs { get; set; }
        public RelayCommand addToCart { get; set; }
        public RelayCommand goToCart { get; set; }
        public RelayCommand login { get; set; }

        public void ShowAll()
        {
            this.CurrentItems = this.allItems;
            RaisePropertyChanged("CurrentItems");
        }

        public void ShowComputers()
        {
            this.CurrentItems = new List<Item>();

            foreach (var item in this.allItems.OfType<Computer>())
            {
                this.CurrentItems.Add(item);
            }
              
            RaisePropertyChanged("CurrentItems");
        }

        public void ShowLaptops()
        {
            this.CurrentItems = new List<Item>();

            foreach (var item in this.allItems.OfType<Laptop>())
            {
                this.CurrentItems.Add(item);
            }

            RaisePropertyChanged("CurrentItems");
        }

        public void ShowGSMs()
        {
            this.CurrentItems = new List<Item>();

            foreach (var item in this.allItems.OfType<GSM>())
            {
                this.CurrentItems.Add(item);
            }

            RaisePropertyChanged("CurrentItems");
        }

        public int CountOfItemsInCart
        {
            get
            {                
                return MainPageViewModel.magazin.Cart.ItemsInCart.Count;                
            }
        }

        public void AddToCart()
        {
            Item itemToAdd = this.CurrentItems[SelectedItem];
            MainPageViewModel.magazin.Cart.AddToCart(itemToAdd);
            RaisePropertyChanged("CountOfItemsInCart");
        }

        private void GoToCart()
        {
            var msg = new GoToPageMessage() { PageName = "CartView" };
            Messenger.Default.Send<GoToPageMessage>(msg);
        }

        private void Login()
        {
            var msg = new GoToPageMessage() { PageName = "LoginView" };
            Messenger.Default.Send<GoToPageMessage>(msg);
        }
    }
}
